using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using EnvDTE;
using VSSDKAssist.Assets.Helpers;
using VSSDKAssist.Assets.Actions.Base;
using System.Globalization;

namespace VSSDKAssist.Recipes.ConfigureSplashAboutScreens
{
	/// <summary>
	/// Action that adds/update the InstalledProductRegistration attribute
	/// </summary>
	public class AddInstalledProductRegistrationAttributeAction : ActionBase
	{
		private CodeClass codeClass;

		/// <summary>
		/// Gets or sets the code class.
		/// </summary>
		/// <value>The code class.</value>
		[Input(Required = true)]
		public CodeClass CodeClass
		{
			get { return codeClass; }
			set { codeClass = value; }
		}

		private string productName;

		/// <summary>
		/// Gets or sets the name of the product.
		/// </summary>
		/// <value>The name of the product.</value>
		[Input(Required = true)]
		public string ProductName
		{
			get { return productName; }
			set { productName = value; }
		}

		private string productNameResourceId;

		/// <summary>
		/// Gets or sets the product name resource id.
		/// </summary>
		/// <value>The product name resource id.</value>
		[Input(Required = true)]
		public string ProductNameResourceId
		{
			get { return productNameResourceId; }
			set { productNameResourceId = value; }
		}

		private string productDetailsResourceId;

		/// <summary>
		/// Gets or sets the product details resource id.
		/// </summary>
		/// <value>The product details resource id.</value>
		[Input(Required = true)]
		public string ProductDetailsResourceId
		{
			get { return productDetailsResourceId; }
			set { productDetailsResourceId = value; }
		}

		private string productVersion;

		/// <summary>
		/// Gets or sets the product version.
		/// </summary>
		/// <value>The product version.</value>
		[Input(Required = true)]
		public string ProductVersion
		{
			get { return productVersion; }
			set { productVersion = value; }
		}

		private string bitmapResourceId;

		/// <summary>
		/// Gets or sets the bitmap resource id.
		/// </summary>
		/// <value>The bitmap resource id.</value>
		[Input(Required = true)]
		public string BitmapResourceId
		{
			get { return bitmapResourceId; }
			set { bitmapResourceId = value; }
		}

		private bool generateSplash;

		/// <summary>
		/// Gets or sets a value indicating whether [generate splash].
		/// </summary>
		/// <value><c>true</c> if [generate splash]; otherwise, <c>false</c>.</value>
		[Input(Required = true)]
		public bool GenerateSplash
		{
			get { return generateSplash; }
			set { generateSplash = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			string attName = "InstalledProductRegistration";
			string attValue =
				string.Format(
					CultureInfo.CurrentCulture, 
					"{0}, \"#{1}\", \"#{2}\", \"{3}\", IconResourceID = {4}, LanguageIndependentName = \"{5}\"",
					this.generateSplash.ToString().ToLower(CultureInfo.CurrentCulture),
					this.ProductNameResourceId,
					this.ProductDetailsResourceId,
					this.ProductVersion,
					this.BitmapResourceId, 
					this.ProductName);

			CodeAttribute defaultRegistryRootAtt = FileCodeModelHelper.GetAttribute(this.codeClass, attName);

			if(defaultRegistryRootAtt != null)
			{
				FileCodeModelHelper.UpdateAttribute(defaultRegistryRootAtt, attValue);
			}
			else
			{
				FileCodeModelHelper.AddAttribute(codeClass, attName, attValue, false);
			}
		}
	}
}